﻿<script type="text/javascript">
    var oldSelectRoleId;
    $(function () {
        getButton("role");
    });
    function ui_role_init_list(toolbar) {
        $("#ui_role_dg").datagrid({       //初始化datagrid
            url: "ashx/rm_role.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
            idField: 'id',
            sortName: 'addDate',
            sortOrder: 'asc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                             {
                                 width: '100',
                                 title: '角色名称',
                                 field: 'name',
                                 sortable: true,
                                 formatter: function (value, row, index) {
                                     return value.length > 8 ? '<span title="' + value + '">' + value + '</span>' : value;
                                 }
                             }]],
            columns: [[
                       { field: 'addDate', title: '添加时间', width: 130 },
                       { field: 'modifyDate', title: '修改时间', width: 130 },
                       {
                           field: 'description', title: '角色描述', width: 300,
                           formatter: function (value, row, index) {
                               return value && value.length > 20 ? '<span title="' + value + '">' + value + '</span>' : value;
                           }
                       }]],
            toolbar: toolbar.length == 0 ? null : toolbar,
            //toolbar: [{
            //    text: '添加',
            //    iconCls: 'icon-add',
            //    handler: function () { ui_role_add(); }
            //}, {
            //    text: '修改',
            //    iconCls: 'icon-edit',
            //    handler: function () { ui_role_edit(); }
            //}, {
            //    text: '删除',
            //    iconCls: 'icon-remove',
            //    handler: function () { ui_role_delete() }
            //}, '-', {
            //    text: '角色授权',
            //    iconCls: 'icon-key',
            //    handler: function () { ui_role_authorize() }
            //}],
            onSelect: function (rowIndex, rowData) {
                if (oldSelectRoleId == rowData.id) {  //点选的是相同的角色就不再请求数据
                    return;
                }
                oldSelectRoleId = rowData.id;   //赋值

                var $ui_role_layout = $("#ui_role_layout");
                var eastRoleUser = $ui_role_layout.layout("panel", "east");

                if (eastRoleUser.panel("options").collapsed) {   //判断是否展开
                    $ui_role_layout.layout("expand", "east");
                }
                eastRoleUser.panel("setTitle", rowData.name + "成员");
                if ($("#ui_role_user_dg").data("datagrid")) {
                    $("#ui_role_user_dg").datagrid({       //请求数据
                        url: "ashx/rm_role.ashx?action=getRoleUser&roleId=" + rowData.id
                    });
                }
                else {
                    $("#ui_role_user_dg").datagrid({       //初始化datagrid
                        url: "ashx/rm_role.ashx?action=getRoleUser&roleId=" + rowData.id,
                        striped: true,
                        rownumbers: true,
                        pagination: true,
                        pageSize: 20,
                        singleSelect: true,
                        idField: 'id',
                        sortName: 'addDate',
                        sortOrder: 'asc',
                        pageList: [20, 40, 60, 80, 100],
                        columns: [[
                              { field: 'userName', title: '登录名', sortable: true, width: 100 },
                              { field: 'realName', title: '姓名', sortable: true, width: 80 },
                              {
                                  field: 'deptNames', title: '部门', width: 150,
                                  formatter: function (value, row, index) {
                                      return value && value.length > 10 ? '<span title="' + value + '">' + value + '</span>' : value;
                                  }
                              },
                              {
                                  field: 'able', title: '启用', sortable: true, width: 60, align: 'center',
                                  formatter: function (value, row, index) {
                                      return value ? '<img src="themes/icons/chk_checked.gif" alt="已启用" title="用户已启用" />' : '<img src="themes/icons/chk_unchecked.gif" alt="未启用" title="用户未启用" />';
                                  }
                              },
                              { field: 'addDate', title: '添加时间', sortable: true, width: 130 }]]
                    });
                }
            }
        });

    }
    //添加角色
    function ui_role_add() {
        $("<div/>").dialog({
            id: "ui_role_add_dialog",
            href: "html/ui_role_edit.html",
            title: "添加角色",
            iconCls: "icon-add",
            height: 250,
            width: 400,
            modal: true,
            buttons: [{
                id: "ui_role_add_btn",
                iconCls: "icon-add",
                text: '添 加',
                handler: function () {
                    $("#ui_role_editform").form("submit", {
                        url: "ashx/rm_role.ashx",
                        onSubmit: function (param) {
                            $('#ui_role_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_role_add_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_role_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_role_dg").datagrid("reload");
                            } else {
                                $('#ui_role_add_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_role_name_edit").focus();
            },
            onClose: function () {
                $("#ui_role_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //修改角色
    function ui_role_edit() {
        var rows = $("#ui_role_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先选择要修改的角色");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改角色");
            $("#ui_role_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_role_edit_dialog",
            href: "html/ui_role_edit.html",
            title: "修改角色",
            iconCls: "icon-edit",
            height: 250,
            width: 400,
            modal: true,
            buttons: [{
                id: "ui_role_edit_btn",
                iconCls: "icon-edit",
                text: '修 改',
                handler: function () {
                    $("#ui_role_editform").form("submit", {
                        url: "ashx/rm_role.ashx",
                        onSubmit: function (param) {
                            $('#ui_role_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_role_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_role_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_role_dg").datagrid("reload");
                            } else {
                                $('#ui_role_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_role_id_edit").val(row.id);
                $("#ui_role_name_edit").textbox("setValue", row.name);
                $("#ui_role_sort_edit").numberspinner("setValue", row.sort);
                $("#ui_role_description_edit").val(row.description);
            },
            onClose: function () {
                $("#ui_role_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //删除角色
    function ui_role_delete() {
        var rows = $("#ui_role_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的角色");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量删除");
            $("#ui_role_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        $.messager.confirm('提示', '确定删除：' + rows.length + '个角色吗？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.ids = ids.join(',');
                $.ajax({
                    url: "ashx/rm_role.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_role_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');;
                            //如果“用户管理”标签页处于打开状态需要刷新，否则被删除的角色还显示，再点编辑用户就出错
                            if ($('#tabs').tabs('exists', '用户管理')) {
                                $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                            }
                            $("#ui_role_layout").layout("collapse", "east");
                            $("#ui_role_user_dg").datagrid('loadData', { total: 0, rows: [] });   //清空
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }
    //角色授权
    function ui_role_authorize() {
        var rows = $("#ui_role_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要授权的角色");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量角色授权");
            $("#ui_role_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        //初始化dialog
        $("<div/>").dialog({
            id: "ui_role_authorize_dialog",
            href: "html/ui_role_authorize.html",
            title: "角色授权：" + row.name,
            iconCls:"icon-authorize",
            height: 500,
            width: 300,
            modal: true,
            buttons: [{
                id: "ui_role_authorize_btn",
                text: '授 权',
                iconCls: "icon-authorize",
                handler: function () {
                    $('#ui_role_authorize_btn').linkbutton('disable');
                    doAuthorize(row.id);  //保存授权信息
                }
            }],
            onLoad: function () {
                $('#tree_menu_button').tree({
                    url: "ashx/rm_menu.ashx?action=getRoleMenu&roleId=" + row.id + "&timespan=" + new Date().getTime(),
                    onlyLeafCheck: true,
                    checkbox: true
                });
            },
            onClose: function () {
                $("#ui_role_authorize_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function doAuthorize(roleId) {   //保存授权信息
        var menuButtonId = "";
        var nodes = $('#tree_menu_button').tree('getChecked');
        var menuIds = [];
        var buttonIds = [];
        for (var i = 0; i < nodes.length; i++) {
            menuIds.push(nodes[i].parentId);    //菜单ID
            buttonIds.push(nodes[i].id);        //按钮ID    
        }
        var para = {};
        para.action = "authorize";
        para.timespan = new Date().getTime();
        para.menuIds = menuIds.join(',');
        para.buttonIds = buttonIds.join(',');
        para.roleId = roleId;

        $.ajax({
            url: "ashx/rm_role.ashx",
            data: para,
            type: "POST",
            dataType: "json",
            success: function (data) {
                if (data.success) {
                    $("#ui_role_authorize_dialog").dialog('close');  //销毁dialog对象
                    $.show_warning("提示", "授权成功！");
                } else {
                    $('#ui_role_authorize_btn').linkbutton('enable');
                    $.show_warning("提示", data.msg);
                }
            }
        });
    }
</script>
<div id="ui_role_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'east',split:true,border:true,collapsed:true" title="角色成员"
        style="width: 480px;">
        <table id="ui_role_user_dg" data-options="fit:true,border:false">
        </table>
    </div>
    <div data-options="region:'center',border:false">
        <table id="ui_role_dg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
